package com.project.scheduled.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.scheduled.aop.LogExecution;
import com.project.scheduled.mapper.*;
import com.project.scheduled.service.BusinessLogicService;
import com.project.scheduled.service.TjDay1LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 定时执行业务逻辑
 */
@Service
@Slf4j
public class BusinessLogicServiceImpl implements BusinessLogicService {
    @Resource
    private GjBusMapper gjBusMapper;
    @Resource
    private GjBusResultMapper gjBusResultMapper;
    @Resource
    private TjDay1LogService tjDay1LogService;
    @Resource
    private RfidRunMapper rfidRunMapper;
    @Resource
    private GjBusCzjczMapper gjBusCzjczMapper;
    @Resource
    private GjBusRealTime2019Mapper gjBusRealTime2019Mapper;

    @Override
    public void test1() {
        log.info("测试1正在执行。。。。。。。。");
        tjDay1LogService.saveLog(1, null, LocalDateTime.now(), "test1");
    }

    @Override
    public void test2() {
        log.info("测试2正在执行。。。。。。。。");
        tjDay1LogService.saveLog(1, null, LocalDateTime.now(), "test2");
    }

    @Override
    public void test3() {
        log.info("测试3正在执行。。。。。。。。");
        tjDay1LogService.saveLog(1, null, LocalDateTime.now(), "test3");
    }

    @Override
    public void test4() {
        log.info("测试4正在执行。。。。。。。。");
        tjDay1LogService.saveLog(1, null, LocalDateTime.now(), "test4");
    }
    /**
     * 线路时刻表每日作业数据
     */
    @LogExecution("XLSKB_START_DAY")
    public void xlskbStartDay(){
        log.info("线路时刻表每日作业数据正在执行。。。。。。。。");
//        try {
//            long startTime = System.currentTimeMillis();
            //保存日志,插入当天开始的日志，step:0
//            tjDay1LogService.saveLog(0, null, LocalDateTime.now(), "XLSKB_START_DAY");
            String day = LocalDate.now().plusDays(-3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //3天前
            //日表
            gjBusMapper.insert_XL_PC_GPS_ZaiXian_day_UPDATE(day);
            //月表
            gjBusMapper.insert_XL_PC_GPS_ZaiXian_month_UPDATE(day);

            //2天前
            day = LocalDate.now().plusDays(-2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //日表
            gjBusMapper.insert_XL_PC_GPS_ZaiXian_day_UPDATE(day);
            //月表
            gjBusMapper.insert_XL_PC_GPS_ZaiXian_month_UPDATE(day);
            gjBusMapper.UPDATE_CZXL_JGXL();
            gjBusMapper.updateJGCLXL();

            //当天
            day = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            gjBusMapper.pro_XLBCSK();
            gjBusMapper.insert_XLSKB_DAY(day);


//            tjDay1LogService.saveLog(1, null, LocalDateTime.now(), "XLSKB_START_DAY");

//            log.info("xlskbStartDay cost time:{}",System.currentTimeMillis()-startTime);
//        } catch (Exception e) {
//           tjDay1LogService.saveLog(9, null, LocalDateTime.now(), "XLSKB_START_DAY执行异常:"+e.getMessage());
//            log.error("XLSKB_START_DAY执行异常",e);
//        }
    }

    @LogExecution("insert_get_user_czcl_liv")
//    @DS("gjbusresult")
    public void insert_get_user_czcl_liv(){
        log.info("insert_get_user_czcl_liv。。。。。。。。");
        gjBusResultMapper.insert_get_user_czcl_liv("jdgj");
        gjBusResultMapper.insert_get_user_czcl_liv("sjgj");

    }

    /**
     * pro_Update_XLSKB_EXTEND
     */
    @LogExecution("pro_Update_XLSKB_EXTEND")
    public void pro_Update_XLSKB_EXTEND(){
        log.info("pro_Update_XLSKB_EXTEND。。。。。。。。");
        gjBusMapper.pro_Update_XLSKB_EXTEND();

    }

    @LogExecution("insert_TB_TRAINING_CERTIFICATE_UID_LIV")
    public void insert_TB_TRAINING_CERTIFICATE_UID_LIV(){
        log.info("insert_TB_TRAINING_CERTIFICATE_UID_LIV。。。。。。。。");
        rfidRunMapper.insert_TB_TRAINING_CERTIFICATE_UID_LIV();

    }

    /**
     * pro_insert_TB_CZCL_RFID_DAY
     */
    @LogExecution("pro_insert_TB_CZCL_RFID_DAY")
    public void pro_insert_TB_CZCL_RFID_DAY(){
        log.info("pro_insert_TB_CZCL_RFID_DAY。。。。。。。。");
        gjBusResultMapper.pro_insert_TB_CZCL_RFID_DAY(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    }

    /**
     * pro_insert_TB_CZXL_RFID_DAY
     */
    @LogExecution("pro_insert_TB_CZXL_RFID_DAY")
    public void pro_insert_TB_CZXL_RFID_DAY(){
        log.info("pro_insert_TB_CZXL_RFID_DAY。。。。。。。。");
        gjBusResultMapper.pro_insert_TB_CZXL_RFID_DAY(LocalDateTime.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    }

    /**
     *backupZdTable
     */
    @LogExecution("backupZdTable")
    public void backupZdTable(){
        log.info("backupZdTable。。。。。。。。");


        gjBusMapper.upd_zd();
        gjBusMapper.insertJG_GJXLZD_BACKUP(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        gjBusMapper.truncateJG_GJXLZD_THE_DAY_BEFORE_YESTERDAY();
        gjBusMapper.insertJG_GJXLZD_THE_DAY_BEFORE_YESTERDAY(LocalDate.now().plusDays(-2).format(DateTimeFormatter.ofPattern("yyyyMMdd")));


    }


    /**
     * backup_clwz_day
     */
    @LogExecution("backup_clwz_day")
    public void backup_clwz_day(){
        log.info("backup_clwz_day。。。。。。。。");
        gjBusCzjczMapper.pro_UpDate_CLWZ();


    }


    /**
     * backup_clwz_tmp
     */
    @LogExecution("backup_clwz_tmp")
    public void backup_clwz_tmp(){
        log.info("backup_clwz_tmp。。。。。。。。");
        gjBusRealTime2019Mapper.pro_Insert_TB_CLWZ();
    }

    /**
     * cleanRawMsg
     */
    @LogExecution("cleanRawMsg")
    public void cleanRawMsg(){
        log.info("cleanRawMsg。。。。。。。。");
        gjBusMapper.cleanRawMsg();


    }
    public static void main(String[] args) {
        System.out.println(LocalDate.now().plusDays(-3));
    }

}
